#!/usr/bin/env python

import wx
import sys
import component

class Messages(component.ListCtrl):
    '''Messages class
    
    @author Julot <andy.julot@gmail.com>
    @since Thursday, February 16, 2012. 10:45 AM'''
    
    _textColour = None
    
    def __init__(self, parent):
        style = wx.LC_REPORT | wx.LC_SINGLE_SEL
        component.ListCtrl.__init__(self, parent, style=style)
        self.InsertColumn(1, 'Date')
        self.InsertColumn(2, 'Time')
        self.InsertColumn(3, 'Sender')
        self.SetColumnWidth(3, wx.LIST_AUTOSIZE)
        self._textColour = self.GetTextColour()
    
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self._onSelected)
        
    _values = []
    
    @property
    def values(self):
        return self._values
    
    @values.setter
    def values(self, values):
        self._values = values
        self.DeleteAllItems()
        for value in values:
            date = value.dateTime.strftime('%Y-%m-%d')
            index = self.InsertStringItem(sys.maxint, date)
            self.SetItemData(index, value.id)
            time = value.dateTime.strftime('%H:%M:%S')
            self.SetStringItem(index, 1, time)
            self.SetStringItem(index, 2, str(value.number))
            if not value.read: self.unread(index)
            else: self.read(index)
    
    def unread(self, index):
        font = self.GetItemFont(index)
        font.SetWeight(wx.BOLD)
        self.SetItemTextColour(index, wx.RED)
        self.SetItemFont(index, font)
    
    def _onSelected(self, event):
        self.read(event.Index)
        event.Skip()
        
    def read(self, index):
        font = self.GetItemFont(index)
        font.SetWeight(wx.NORMAL)
        self.SetItemTextColour(index, self._textColour)
        self.SetItemFont(index, font)
    
    def delete(self, index):
        pass